/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-12-22 20:11:14
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2025-07-16 07:42:55
 * @FilePath: \wire_receiver_collect_box\communication\com_lora.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "com_lora.h"
#include "com_scan.h"
#include "crc16.h"
#include <string.h>

/* 设置数据 */
struct scan_set_info_t scan_set_info = {
    .drv1_stop_line_len = 0,
    .drv1_slow_line_len = 0,
    .drv2_stop_line_len = 0,
    .drv2_slow_line_len = 0,
};

static uint8_t com_scan_recv_null(uint8_t *buff, uint8_t len) { return 0; }
static uint8_t com_scan_recv_haert1(uint8_t *buff, uint8_t len);
static uint8_t com_scan_recv_haert2(uint8_t *buff, uint8_t len);
static uint8_t com_scan_recv_key_ctrl(uint8_t *buff, uint8_t len);

typedef uint8_t (*function)(uint8_t *buff, uint8_t len);
static const function list_cmd_function[] = {
    com_scan_recv_haert1, // 上位机下发的线长数据
    com_scan_recv_haert2,
    com_scan_recv_key_ctrl,
    com_scan_recv_null,
};

///////////////////////////////////////////////////////////////////////////////////////
//                 以下是接收数据处理
///////////////////////////////////////////////////////////////////////////////////////
static uint8_t com_scan_recv_haert1(uint8_t *buff, uint8_t len)
{
    master_lora.flag_on_use = 1;
    master_lora.key[0].start_ctrl = 0x00;
    master_lora.key[0].slow_ctrl = 0x00;
    master_lora.key[0].stop_ctrl = 0x00;

    scan_set_info.drv1_stop_line_len =
        buff[0] << 24 | buff[1] << 16 | buff[2] << 8 | buff[3] << 0;
    scan_set_info.drv1_slow_line_len =
        buff[4] << 24 | buff[5] << 16 | buff[6] << 8 | buff[7] << 0;

    SEGGER_RTT_printf(0, "drv1_len = %d;\r\n",
                      scan_set_info.drv1_stop_line_len,
                      scan_set_info.drv1_slow_line_len);
    return 0;
}
static uint8_t com_scan_recv_haert2(uint8_t *buff, uint8_t len)
{
    master_lora.flag_on_use = 1;
    master_lora.key[1].start_ctrl = 0x00;
    master_lora.key[1].slow_ctrl = 0x00;
    master_lora.key[1].stop_ctrl = 0x00;

    scan_set_info.drv2_stop_line_len = \
        buff[0] << 24 | buff[1] << 16 | buff[2] << 8 | buff[3] << 0;
    scan_set_info.drv2_slow_line_len = \
        buff[4] << 24 | buff[5] << 16 | buff[6] << 8 | buff[7] << 0;

    SEGGER_RTT_printf(0, "drv2_len = %d;\r\n",
                      scan_set_info.drv2_stop_line_len,
                      scan_set_info.drv2_slow_line_len);
    return 0;
}
static uint8_t com_scan_recv_key_ctrl(uint8_t *buff, uint8_t len)
{
    switch (buff[0])
    {
    case 0x01:
        master_lora.flag_on_use = 1;
        master_lora.key[0].start_ctrl = 0x01;
        master_lora.key[0].slow_ctrl = 0x00;
        master_lora.key[0].stop_ctrl = 0x00;
        break;
    case 0x02:
        master_lora.flag_on_use = 1;
        master_lora.key[0].start_ctrl = 0x00;
        master_lora.key[0].slow_ctrl = 0x00;
        master_lora.key[0].stop_ctrl = 0x01;
        break;
    case 0x04:
        master_lora.flag_on_use = 1;
        master_lora.key[0].start_ctrl = 0x00;
        master_lora.key[0].slow_ctrl = 0x01;
        master_lora.key[0].stop_ctrl = 0x00;
        break;
    default:
        master_lora.flag_on_use = 0;
        break;
    }

    SEGGER_RTT_printf(0, "drv2_len = 0x%x .\r\n", buff[0]);
    return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//                 以下是给屏幕更新数据
///////////////////////////////////////////////////////////////////////////////////////
static struct _protocol pack; /* 缓存一帧数据 */
void scan_receive_analysis(uint8_t byte)
{
    static uint8_t number = 0;
    static uint8_t pos = 0;
    uint16_t cal_crc = 0;

LAB_ANALYSIS:
    switch (number)
    {
    case 0: /* 找帧头1 */
        if (FREAME_HANDER_AD == byte)
        {
            number = 1;
        }
        break;
    case 1: /* 找帧头2 */
        if (FREAME_HANDER_DA == byte)
        {
            number = 2;
            pos = 0;
        }
        else
        {
            number = 0;
            goto LAB_ANALYSIS;
        }
        break;
    case 2: /* 获取地址 */
        pack.buff[pos++] = byte;
        number = 3;
        break;
    case 3: /* 获取长度 */
        pack.size = byte;
        pack.buff[pos] = byte;
        pos++;
        number = 4;
        break;
    case 4: /* 获取类型 */
        pack.buff[pos] = byte;
        pos++;
        number = 5;
        break;
    case 5: /* 获取数据 */
        pack.buff[pos] = byte;
        if (++pos >= (pack.size + 4)) /* +2是为了获取校验 */
        {
            uint16_t recv_crc = pack.buff[pos - 2]; // pack.buff[pack.size + 3] | (byte << 8);
            cal_crc = crc8_checksum((uint8_t *)(&pack.buff), (pos - 2));
            if (cal_crc == recv_crc)
            {
                list_cmd_function[pack.buff[2]](&pack.buff[3], (pack.size - 1));
            }

            number = 0;
        }
        break;
    default:
        break;
    }
}

/*******************************************************************
 * 函数名: com_scan_send_change_sensor()
 * 功 能: 带颜色传感器数据
 * 输 入: uint8_t id : 设备id号
 *
 * 输 出:
 *******************************************************************/
static struct _protocol pack;
static struct scan_senser_drv_t senser_drv = {
    .drv1_state = 1,
    .drv2_state = 1,
};
int16_t com_scan_send_change_sensor(void)
{
    pack.buff[0] = FREAME_HANDER_AD;
    pack.buff[1] = FREAME_HANDER_DA;
    memcpy(&pack.buff[2], &(senser_drv), sizeof(senser_drv));
    uint8_t len = sizeof(senser_drv); // 10

    uint16_t crc = crc16_checksum(&pack.buff[2], len);

    pack.buff[2 + len + 1] = (uint8_t)((crc >> 8) & 0x00ff);
    pack.buff[2 + len] = (uint8_t)(crc & 0x00ff);

    pack.size = (2 + len + 2);

    bsp_uart_driver_t *drv = scan_drv();
    drv->ops.ioctrl(drv, IOCTRL_SET_TX_BUFF, (uint32_t *)(&pack));

    return 0;
}
